<?php
    class task_model extends Model
    {
        const PTN = '<li><a href="#" rel="%s" >%s</a></li>';

        public function __construct(){
            parent::__construct();
            $this->setTableName('tasks');
        }

		/*
			@param int id of task to generate sub task list
			@return string sub task list in html form
		*/
        public function generateChild($id = null){
            //fetch info from database using id
            //assert id as unsigned number
            $id = intval($id);

            $data = null;

            if (empty($id)){
                //select info that parent task is null means a standalone project main task
                $data = $this->db->fetch(
                	'select id,name from ' . $this->getTableName()
                    . ' where parent_task is NULL');
            }
            else{
                $data = $this->select(array('id', 'name'), array('parent_task' => $id));
            }

            $retStr = '';

            if (!empty($data)){
                $retStr .= '<ul>';
                foreach($data as $v){
                    //iterate every value and combine pattern with values
                    $retStr .= sprintf(self::PTN, $v['id'], $v['name']);
                }
                $retStr .= '</ul>';
            }

            return $retStr;
        }

		/*
			@param id int id of task to fetch information
			@return array data of record store in database
		*/
        public function fetchTaskInfo($id){
            //assert id as int
            $id = intval($id);

            if (empty($id)){
                return false;
            }
            
            $sql = 'select * from ' . $this->getTableName()
            	 . ' where id = {0} limit 1';
        	
        	return $this->db->fetchOne($sql, array($id));
        }

		/*
			@return array list of tasks stored in database
		*/
        public function fetchTaskList(){
            //fetch task list
            return $this->select(array('id', 'name'));
        }
        
        /*
        	@param array list of options of a adding task
            @return boolean state of current adding task
        */
        public function addTask($options){
        	//assertions
            if (isset($options['parent_task'])){
            	if (empty($options['parent_task']))
                	$options['parent_task'] = null;
                else{
                	//check existence
                    $options['parent_task'] = intval($options['parent_task']);
                    if (!$this->exists($options['parent_task']))
                    	return false;
                }
            }
            else
            	$options['parent_task'] = null;
                
            if (isset($options['creator_user_id'])){
            	//existence checking
                $options['creator_user_id'] = intval($options['creator_user_id']);
                if (!$this->load->user('model')->exists($options['creator_user_id']))
                	return false;
            }
            else{
            	return false;
            }
            	
            //insert
            return $this->insert($options);
        }
        
        /*
    		@param int id of task
        	@return bool state of enable action	
        */
        public function enable($id){
        	return $this->_dispose(intval($id), true);
        }
        
        /*
        	@param int id of task
        	@return bool state of disable action
        */
        public function disable($id){
        	return $this->_dispose(intval($id), false);
        }
        
        /*
        	@param int id of task
            @return bool existence of a task
        */
        public function exists($id){
        	$info = $this->fetchTaskInfo($id);
            
            return !empty($info);
        }
        
        /*
        	@param bool state of task dispose 
            @return bool bit set success state
        */
        private function _dispose($id, $state){
            if (!$this->exists($id)) 
            	return false;
            else {
            	$state = $state ? 1 : 0;
            	return $this->update(
                	array('disposed' => $state), array('id' => $id));
            }
        }
    }